package com.afkt.project.ui.slice;


import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.ui.ShapeImage;
import com.afkt.project.util.LogUtils;
import com.ohos.afkt.ResourceTable;
import com.dev.base.widget.BaseTextView;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.multimodalinput.event.TouchEvent;

/**
 * detail: Material BottomSheetDialog
 * @author Ttt
 */
public class BottomSheetDialogActivity extends BaseActivity implements Component.TouchEventListener {
    private static final String TAG = "BottomSheetDialogActivity";

    private ShapeImage image1;
    private ShapeImage image2;
    private ShapeImage image3;
    private ShapeImage image4;
    private ShapeImage image5;
    private ShapeImage image6;
    private ShapeImage image7;
    private ShapeImage image8;
    private ShapeImage image9;

    private ShapeImage image10;
    private ShapeImage image11;
    private ShapeImage image12;
    private ShapeImage image13;
    private ShapeImage image14;
    private ShapeImage image15;
    private ShapeImage image16;
    private ShapeImage image17;
    private ShapeImage image18;

    private DirectionalLayout bar_Layout;
    private DirectionalLayout bar;
    private Image sheetdialog_return;
    private BaseTextView bottomSheetDialog;

    //dialog 页面效果
    private DirectionalLayout shapeableImageView_dialog;
    private DirectionalLayout.LayoutConfig layoutConfig;
    private ScrollView scroll_one;
    private Component grey;

    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_activity_bottom_sheet_dialog;
    }

    @Override
    protected void initView() {


        image1 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_1);
        image2 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_2);
        image3 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_3);
        image4 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_4);
        image5 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_5);
        image6 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_6);
        image7 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_7);
        image8 = (ShapeImage) findComponentById(ResourceTable.Id_shapeIamge_8);
        image9 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_9);

        image10 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_10);
        image11 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_11);
        image12 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_12);
        image13 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_13);
        image14 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_14);
        image15 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_15);
        image16 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_16);
        image17 = (ShapeImage) findComponentById(ResourceTable.Id_shapeIamge_17);
        image18 = (ShapeImage) findComponentById(ResourceTable.Id_shapeImage_18);


        image1.setPixelMapAndCircle(ResourceTable.Media_bg_wallpaper);
        image2.setPixelMapAndRoundRectAndStroke(ResourceTable.Media_bg_wallpaper, 60);
        image3.setPixelMapClip(ResourceTable.Media_bg_wallpaper, 70);
        image4.setPixelMapCube(ResourceTable.Media_bg_wallpaper);
        image5.setPixelMapSector(ResourceTable.Media_bg_wallpaper);
        image6.setPixelMapTwoClip(ResourceTable.Media_bg_wallpaper);
        image7.setPixelMapBesselCurve(ResourceTable.Media_bg_wallpaper);
        image8.setPixelMapOvalAndClip(ResourceTable.Media_bg_wallpaper);
        image9.setPixelMapTwoClipAndRadius(ResourceTable.Media_bg_wallpaper, 60);


        image10.setPixelMapAndCircle(ResourceTable.Media_bg_wallpaper);
        image11.setPixelMapAndRoundRectAndStroke(ResourceTable.Media_bg_wallpaper, 60);
        image12.setPixelMapClip(ResourceTable.Media_bg_wallpaper, 70);
        image13.setPixelMapCube(ResourceTable.Media_bg_wallpaper);
        image14.setPixelMapSector(ResourceTable.Media_bg_wallpaper);
        image15.setPixelMapTwoClip(ResourceTable.Media_bg_wallpaper);
        image16.setPixelMapBesselCurve(ResourceTable.Media_bg_wallpaper);
        image17.setPixelMapOvalAndClip(ResourceTable.Media_bg_wallpaper);
        image18.setPixelMapTwoClipAndRadius(ResourceTable.Media_bg_wallpaper, 60);


        bar_Layout = (DirectionalLayout) findComponentById(ResourceTable.Id_sheetdialog_barLayout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_sheet_dialog_bar, null, false);
        bar_Layout.addComponent(bar);
        sheetdialog_return = (Image) bar.findComponentById(ResourceTable.Id_sheet_dialog_return);

        shapeableImageView_dialog = (DirectionalLayout) findComponentById(ResourceTable.Id_shapeableImageView_dialog);
        layoutConfig = new DirectionalLayout.LayoutConfig();
        layoutConfig.setMarginTop(1050);
        shapeableImageView_dialog.setLayoutConfig(layoutConfig);

        bottomSheetDialog = (BaseTextView) findComponentById(ResourceTable.Id_bottomSheetDialog);
        bottomSheetDialog.setTouchEventListener(this);
        bottomSheetDialog.setTouchEventListener(this);

        scroll_one = (ScrollView) findComponentById(ResourceTable.Id_bottomShettDialog_scroll_one);

        grey = findComponentById(ResourceTable.Id_grey_background);
        grey.setVisibility(Component.VISIBLE);
        //createDialog();
    }

    private void createDialog() {
        Component component = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_bottom_sheet_dialog, null, false);
        CommonDialog commonDialog = new CommonDialog(this);
        commonDialog.setContentCustomComponent(component);
        commonDialog.setSize(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
        commonDialog.setAlignment(LayoutAlignment.LEFT | LayoutAlignment.TOP);
        commonDialog.setOffset(0, 1000);
        commonDialog.setSwipeToDismiss(true);
        commonDialog.siteRemovable(true);
        commonDialog.setAutoClosable(true);
        commonDialog.show();

        image10 = (ShapeImage) component.findComponentById(ResourceTable.Id_shapeImage_10);
        image11 = (ShapeImage) component.findComponentById(ResourceTable.Id_shapeImage_11);
        image12 = (ShapeImage) component.findComponentById(ResourceTable.Id_shapeImage_12);
        image13 = (ShapeImage) component.findComponentById(ResourceTable.Id_shapeImage_13);
        image14 = (ShapeImage) component.findComponentById(ResourceTable.Id_shapeImage_14);
        image15 = (ShapeImage) component.findComponentById(ResourceTable.Id_shapeImage_15);
        image16 = (ShapeImage) component.findComponentById(ResourceTable.Id_shapeImage_16);
        image17 = (ShapeImage) component.findComponentById(ResourceTable.Id_shapeIamge_17);
        image18 = (ShapeImage) component.findComponentById(ResourceTable.Id_shapeImage_18);

        image10.setPixelMapAndCircle(ResourceTable.Media_bg_wallpaper);
        image11.setPixelMapAndRoundRectAndStroke(ResourceTable.Media_bg_wallpaper, 60);
        image12.setPixelMapClip(ResourceTable.Media_bg_wallpaper, 70);
        image13.setPixelMapCube(ResourceTable.Media_bg_wallpaper);
        image14.setPixelMapSector(ResourceTable.Media_bg_wallpaper);
        image15.setPixelMapTwoClip(ResourceTable.Media_bg_wallpaper);
        image16.setPixelMapBesselCurve(ResourceTable.Media_bg_wallpaper);
        image17.setPixelMapOvalAndClip(ResourceTable.Media_bg_wallpaper);
        image18.setPixelMapTwoClipAndRadius(ResourceTable.Media_bg_wallpaper, 60);
    }

    @Override
    protected void initListener() {
        sheetdialog_return.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onBackPressed();
            }
        });

        scroll_one.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //点击消失
                grey.setVisibility(Component.HIDE);
                layoutConfig.setMarginTop(2340);
                shapeableImageView_dialog.setLayoutConfig(layoutConfig);
            }
        });


    }

    @Override
    protected void onBackground() {
        bar_Layout.removeComponent(bar);
        super.onBackground();
    }

    private int oldX;
    private int oldY;

    private int moveX;
    private int moveY;

    private boolean isTop = false;
    private boolean isBottom = true;
    //向上滑动
    private boolean isHalfUp = false;
    //向下滑动
    private boolean isHalfDown = false;

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (component.getId()) {
            case ResourceTable.Id_bottomSheetDialog:
                switch (touchEvent.getAction()) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        LogUtils.error(TAG, "按下");
                        //按下
                        oldX = (int) touchEvent.getPointerScreenPosition(touchEvent.getPointerId(0)).getX();
                        oldY = (int) touchEvent.getPointerScreenPosition(touchEvent.getPointerId(0)).getY();
                        break;
                    case TouchEvent.POINT_MOVE:
                        LogUtils.error(TAG, "移动");
                        moveX = (int) touchEvent.getPointerScreenPosition(touchEvent.getPointerId(0)).getX();
                        moveY = (int) touchEvent.getPointerScreenPosition(touchEvent.getPointerId(0)).getY();
                        LogUtils.error(TAG, "moveY = " + moveY);
                        // 计算距离，以及方向，
                        int distanceX = moveX - oldX;
                        int distanceY = moveY - oldY;
                        if (distanceY > 0) {
                            isHalfDown = true;
                            isHalfUp = false;
                        } else {
                            isHalfUp = true;
                            isHalfDown = false;
                        }
                        LogUtils.error(TAG, "distanceX = " + distanceX + "distanceY =" + distanceY);
                        ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig();
                        if (isBottom && isHalfDown && moveY > 1050) {
                            //在底部时 向下滑动
                            layoutConfig.setMarginTop(distanceY + 1050);
                            shapeableImageView_dialog.setLayoutConfig(layoutConfig);
                        }

                        if (isHalfUp) {
                            layoutConfig.setMarginTop(distanceY + 1050);
                            shapeableImageView_dialog.setLayoutConfig(layoutConfig);
                        }

                        if (isHalfUp && (distanceY < -400 || moveY < 400)) {
                            layoutConfig.setMarginTop(0);
                            shapeableImageView_dialog.setLayoutConfig(layoutConfig);
                            bar_Layout.setVisibility(Component.HIDE);
                            grey.setVisibility(Component.VISIBLE);
                            isTop = true;
                            isBottom = false;
                        }

                        if (isTop && isHalfDown) {
                            bar_Layout.setVisibility(Component.VISIBLE);
                            grey.setVisibility(Component.VISIBLE);
                            layoutConfig.setMarginTop(distanceY+180);
                            shapeableImageView_dialog.setLayoutConfig(layoutConfig);
                        }

                        break;
                    case TouchEvent.PRIMARY_POINT_UP:
                        LogUtils.error(TAG, "抬起");
                        grey.setVisibility(Component.HIDE);
                        ComponentContainer.LayoutConfig layoutConfig1 = new ComponentContainer.LayoutConfig();
                        //抬起 消失
                        if (shapeableImageView_dialog.getLayoutConfig().getMarginTop() > 1500) {
                            //底部时 继续向下滑动到一半90 之后消失
                            layoutConfig1.setMarginTop(2340);
                            shapeableImageView_dialog.setLayoutConfig(layoutConfig1);
                            scroll_one.setMarginBottom(0);
                        } else if (shapeableImageView_dialog.getLayoutConfig().getMarginTop() < 400) {
                            //滑动不到800 之后重新返回顶部
                            layoutConfig1.setMarginTop(0);
                            bar_Layout.setVisibility(Component.HIDE);
                            isTop = true;
                            isBottom = false;
                            shapeableImageView_dialog.setLayoutConfig(layoutConfig1);
                        } else if (shapeableImageView_dialog.getLayoutConfig().getMarginTop() > 400) {
                            //滑动到800 之后重新返回顶部
                            layoutConfig1.setMarginTop(1050);
                            grey.setVisibility(Component.VISIBLE);
                            isBottom = true;
                            isTop = false;
                            shapeableImageView_dialog.setLayoutConfig(layoutConfig1);
                        }


                        break;
                }
                break;
        }
        return false;
    }
}
